package com.neusoft.neuiotms.dcvm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.neuiotms.dcvm.bean.StandardCiBean;
import com.neusoft.neuiotms.dcvm.bean.TopoBean;
import com.neusoft.neuiotms.dcvm.service.BusinessService;
import com.neusoft.neuiotms.dcvm.utils.CmdbApi;

@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private RestTemplate restTemplate;
    @Value("${cmdb.proxy.target-url}")
    private String cmdbServiceUrl;

    @Override
    public TopoBean getBusinessTopo(final String businessCode) {
        final ParameterizedTypeReference<ResultDTO<TopoBean>> typeRef =
                new ParameterizedTypeReference<ResultDTO<TopoBean>>() {
                };

        final ResponseEntity<ResultDTO<TopoBean>> response = this.restTemplate
                .exchange(this.cmdbServiceUrl + String.format(CmdbApi.businessTopoUrl, businessCode), HttpMethod.GET,
                        null,
                        typeRef);
        return response.getBody().getData();
    }

    @Override
    public List<StandardCiBean> getBusinessAssets(final String businessCode) {
        final ParameterizedTypeReference<ResultListDTO<StandardCiBean>> typeRef =
                new ParameterizedTypeReference<ResultListDTO<StandardCiBean>>() {
                };

        final String uri = this.cmdbServiceUrl + String.format(CmdbApi.businessInstancesUrl, businessCode);

        final ResponseEntity<ResultListDTO<StandardCiBean>> response = this.restTemplate
                .exchange(uri, HttpMethod.GET,
                        null,
                        typeRef);
        return response.getBody().getData();
    }

}
